const localData = localStorage.getItem('admin')

const state = localData ? localData.menu : {
    isCollapse: false,
    selectMenu: [],
    routerList: [],
    menuActive: '/dashboard'
}

const mutations = {
    collapseMenu(state) {
        state.isCollapse = !state.isCollapse
    },
    addMenu(state, val) {
        state.selectMenu.findIndex(item => item.path === val.path) === -1 ? state.selectMenu.push(val) : ''
    },
    closeMenu(state, payload) {
        const index = state.selectMenu.findIndex(item => item.path === payload.item.path);
        state.selectMenu.splice(index, 1);
    },
    dynamicMenu(state, val) {
        //通过glob引入文件
        const modules = import.meta.glob('../views/**/**/*.vue')
        console.log('Available modules:', modules)
        console.log('Menu data:', val)

        function routerSet(routerList) {
            // 修复：正确遍历数组
            routerList.forEach(route => {
                //判断是否有子菜单，没有子菜单则拼接路由数据
                if (!route.children) { 
                    const url = `../views${route.meta.path}.vue`
                    route.component = modules[url]
                }else{
                    routerSet(route.children)
                }
            })
        }
        routerSet(val)
        state.routerList = val
    },
    updateMenuActive(state, val){
        state.menuActive = val
    }

}

export default {
    state,
    mutations
}